package cc.rengu.igas.share.core.realize.impl;

import cc.rengu.igas.share.common.dao.HolidayInfoMapper;
import cc.rengu.igas.share.common.dao.RoleAcctInfoMapper;
import cc.rengu.igas.share.common.dao.impl.HolidayInfoMapperImpl;
import cc.rengu.igas.share.common.dao.impl.RoleAcctInfoMapperImpl;
import cc.rengu.igas.share.common.entity.RoleAcctInfo;
import cc.rengu.igas.share.core.realize.CsmcService;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 清分清算服务组件实现类
 * Created by wangcheng on 2021/6/29 11:10
 */
public class CsmcServiceImpl implements CsmcService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String callMode = "0";

    public CsmcServiceImpl() {
        String callMode = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.APPLICATION_CONF_NODE, AppConfigConstant.CSMC_SERVICE_CALL_MODE);
        if (!StringUtil.isEmptyOrNull(callMode)) {
            if (!"0".equals(callMode.trim()) && !"1".equals(callMode.trim())) {
                rglog.error("清分清算相关获取调用模式<{}>参数配置错误:0-本地调用，1-微服务调用!", callMode.trim());
                throw new BizException("商户服务相关信息获取方式配置有误:<{}>", callMode);
            } else {
                this.callMode = callMode.trim();
            }
        }
    }

    @Override
    public boolean isHoliday(String date) throws Exception {
        if ("0".equals(callMode)) {
            HolidayInfoMapper holidayInfoMapper = new HolidayInfoMapperImpl();
            return holidayInfoMapper.isHoliday(date);
        } else {
            //TODO
            rglog.info("待实现调用批量服务获取对应信息，默认返回不是节假日!");
            return false;
        }
    }

    @Override
    public String getRoleActualAcctNo(String instId, String settleAcctId, String roleId) throws Exception {
        if ("0".equals(callMode)) {
            RoleAcctInfoMapper roleAcctInfoMapper = new RoleAcctInfoMapperImpl();
            if (StringUtil.isEmptyOrNull(roleId)) {
                roleId = instId + "777777";
            }
            RoleAcctInfo roleAcctInfo = roleAcctInfoMapper.selectBankRoleAcctInfoBySettleAcctId(instId, settleAcctId, roleId);
            return (null == roleAcctInfo) ? null : roleAcctInfo.getActualAcct();
        } else {
            //TODO
            rglog.info("待实现调用批量服务获取对应信息，默认返回空!");
            return null;
        }
    }
}
